package com.autumn.platform.core.context.response.impl;

import java.util.Locale;

import com.autumn.platform.core.context.response.IResponse;
import com.autumn.platform.core.context.track.Tracker;
import com.autumn.platform.core.exception.CodableException;
import com.autumn.platform.core.exception.Throws;

public class Response implements IResponse {

    private static final long serialVersionUID = -6481341673773345333L;

    private String code;

    private String message;

    private boolean success;

    private String trackId;

    public Response() {
        this.code = SUCCESS_CODE;
        this.message = SUCCESS_MESSAGE;
        this.success = true;
        this.trackId = Tracker.getCurrentTrackId();
    }

    public Response(String code, String message) {
        this.code = code;
        this.message = message;
        this.success = isSuccessCode(code);
        this.trackId = Tracker.getCurrentTrackId();
    }

    @Override
    public String getCode() {
        return code;
    }

    @Override
    public void setCode(String code) {
        this.code = code;
        this.success = isSuccessCode(code);
        if (!this.success && SUCCESS_MESSAGE.equalsIgnoreCase(message)) {
            this.message = null;
        }
    }

    @Override
    public void setExceptionCode(String code, Object... args) {
        this.setExceptionCode(code, null, args);
    }

    @Override
    public void setExceptionCode(String code, Locale locale, Object... args) {
        if (this.isSuccessCode(code)) {
            throw new IllegalArgumentException("the code [" + code + "] is success code, please check the exception code...");
        }
        this.code = code;
        this.success = false;
        this.message = Throws.getMessage(code, locale, args);
    }

    @Override
    public String getTrackId() {
        return trackId;
    }

    @Override
    public void setTrackId(String trackId) {
        this.trackId = trackId;
    }

    @Override
    public String getMessage() {
        return message;
    }

    @Override
    public void setMessage(String message) {
        this.message = message;
    }

    @Override
    public boolean isSuccess() {
        return success;
    }

    protected boolean isSuccessCode(String code) {
        return SUCCESS_CODE.equals(code);
    }

    @Override
    public void setSuccess(boolean success) {
        if (success) {
            if (!this.isSuccessCode(this.code)) {
                this.code = SUCCESS_CODE;
            }
            this.success = true;
        } else {
            if (this.isSuccessCode(this.code)) {
                this.code = null;
            }
            this.success = false;
        }
    }

    @Override
    public CodableException toException() {
        if (this.isSuccess()) {
            return null;
        }
        return Throws.createCodeAndMessageException(code, message);
    }
}
